package code

import (
	"net/http"

	"github.com/mars/third_part/fork/errors"
)

type ErrCode struct {
	C          int
	StatusCode int
	Ext        string
	Ref        string
}

var _ errors.Coder = (*ErrCode)(nil)

func (ec *ErrCode) HTTPStatus() int {
	if ec.StatusCode == 0 {
		return http.StatusInternalServerError
	}

	return ec.StatusCode
}

func (ec *ErrCode) String() string {
	return ec.Ext
}

func (ec *ErrCode) Reference() string {
	return ec.Ref
}

func (ec *ErrCode) Code() int {
	return ec.C
}

func register(code int, httpStatus int, message string, refs ...string) {
	// TODO 校验httpStatus是否符合规范，目前不校验
	var refrence string

	if len(refs) > 0 {
		refrence = refs[0]
	}
	coder := &ErrCode{
		C:          code,
		StatusCode: httpStatus,
		Ext:        message,
		Ref:        refrence,
	}
	errors.Register(coder)
}
